package com.guet.niit.Login.controller.agent;

import com.guet.niit.Agent.pojo.Agent;
import com.guet.niit.Agent.service.AgentService;
import com.guet.niit.Login.pojo.ProfitData;
import com.sun.javafx.stage.StageHelper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class ProfitRecordFxController implements Initializable {
    public static ProfitRecordFxController profitRecordFxController;

    @FXML
    private ImageView backtrack;

    @FXML
    private TableView<ProfitData> tableView;
    private ObservableList<ProfitData> data = FXCollections.observableArrayList();

    @FXML
    private TableColumn<ProfitData, Long> tc_id;

    @FXML
    private TableColumn<ProfitData, Double> tc_profit;

    @FXML
    private TableColumn<ProfitData, Long> tc_station;

    @FXML
    private TableColumn<ProfitData, LocalDateTime> tc_updateTime;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        profitRecordFxController = this;
    }

    /**
     * 返回菜单
     * @param mouseEvent
     */
    public void backtrack(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/agent/agentMenu.fxml"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));
    }

    /**
     * 读取利润报表
     */
    public void readProfit(){
        //获取当前登录用户的id
        String name = Thread.currentThread().getName();
        long agentId = Long.parseLong(name);

        //调用service层查询利润报表
        AgentService agentService = new AgentService();
        List<Agent> profits = agentService.Profit_Found(agentId);

        //绑定数据模型
        tc_id.setCellValueFactory(new PropertyValueFactory<>("id"));
        tc_profit.setCellValueFactory(new PropertyValueFactory<>("profit"));

        //获取利润报表数据(获取有问题，有时间再改)
        for (int i = 0; i < profits.size(); i++) {
            Long id = profits.get(i).getId();
            Integer profit = profits.get(i).getProfit();
            Long stationId = profits.get(i).getStation_id();
            Date updateTime = profits.get(i).getUpdate_time();
//            data.add(new ProfitData(id,profit,stationId,updateTime));
//            tc_id.setCellValueFactory(cellData -> cellData.getValue().idProperty());
        }



    }


}
